<?php

/**
 * This is the model class for table "tbl_user_role".
 *
 * The followings are the available columns in table 'tbl_user_role':
 * @property string $id
 * @property string $user_id
 * @property string $user_type_id
 */
class UserRole extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_user_role';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, user_type_id', 'required'),
			array('user_id, user_type_id', 'length', 'max'=>20),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, user_id, user_type_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'user_type_id' => 'User Type',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('user_type_id',$this->user_type_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return UserRole the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        public function userNames()
        {
    //        $data=  User::model()->findAll('parent_id=:parent_id', 
    //                  array(':parent_id'=>(int) $_POST['country_id']));
            
            //$model=User::getFields();
        /* @var $data type */
            Yii::import('application.modules.user.*');
            Yii::import('application.modules.user.models.*');
            $data = UserModule::user()->model()->findAll();

            $data=CHtml::listData($data,'id','username');
            foreach($data as $value=>$name)
            {
                echo CHtml::tag('option',
                       array('value'=>$value),CHtml::encode($name),true);
            }
        }
        public function getUserNames($pk)
        {
            Yii::import('application.modules.user.*');
            Yii::import('application.modules.user.models.*');
            $data = UserModule::user()->model()->findByPk($pk);
            return $data->username;
        }
        //ivan
        public function getUserTypes($pk)
        {
            $userTypeOb=  UserType::model()->findByPk($pk);
            return $userTypeOb->type;
        }
        public function getUserNamesNotExists()
        {
            Yii::import('application.modules.user.*');
            Yii::import('application.modules.user.models.*');
            $sql = 'SELECT id,username FROM tbl_users tbu 
                WHERE  NOT EXISTS (SELECT * FROM   tbl_user_role tbur
                   WHERE  tbu.id = tbur.user_id) ';
            $data = UserModule::user()->model()->findAllBySql($sql);
            return $data;
//            $connection = Yii::app()->db;
//            $command = $connection->createCommand($sql);
//            $results = $command->queryAll();
        }
}
